{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de53995b-32ed-4722-8cac-ba104c8efacb",
   "metadata": {},
   "source": [
    "# 导入环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52fac949-4150-4091-b0c3-2968ab5e385c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/CV/anaconda3/envs/llm23/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from datasets import Dataset\n",
    "import pandas as pd\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e098d9eb",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将JSON文件转换为CSV文件\n",
    "df = pd.read_json('../../dataset/huanhuan.json')\n",
    "ds = Dataset.from_pandas(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8ac92d42-efae-49b1-a00e-ccaa75b98938",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'instruction': ['小姐，别的秀女都在求中选，唯有咱们小姐想被撂牌子，菩萨一定记得真真儿的——',\n",
       "  '这个温太医啊，也是古怪，谁不知太医不得皇命不能为皇族以外的人请脉诊病，他倒好，十天半月便往咱们府里跑。',\n",
       "  '嬛妹妹，刚刚我去府上请脉，听甄伯母说你来这里进香了。'],\n",
       " 'input': ['', '', ''],\n",
       " 'output': ['嘘——都说许愿说破是不灵的。', '你们俩话太多了，我该和温太医要一剂药，好好治治你们。', '出来走走，也是散心。']}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51d05e5d-d14e-4f03-92be-9a9677d41918",
   "metadata": {},
   "source": [
    "# 处理数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "74ee5a67-2e55-4974-b90e-cbf492de500a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using bos_token, but it is not set yet.\n",
      "Using eos_token, but it is not set yet.\n",
      "Using unk_token, but it is not set yet.\n",
      "Using sep_token, but it is not set yet.\n",
      "Using cls_token, but it is not set yet.\n",
      "Using mask_token, but it is not set yet.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "QWenTokenizer(name_or_path='/CV/xhr_project/llm/model/Qwen-7B-Chat', vocab_size=151851, model_max_length=32768, is_fast=False, padding_side='right', truncation_side='right', special_tokens={'pad_token': '<|endoftext|>'}, clean_up_tokenization_spaces=True),  added_tokens_decoder={\n",
       "\t\n",
       "}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('./qwen/Qwen-7B-Chat/', use_fast=False, trust_remote_code=True)\n",
    "tokenizer.pad_token_id = tokenizer.eod_id\n",
    "tokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2503a5fa-9621-4495-9035-8e7ef6525691",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def process_func(example):\n",
    "    MAX_LENGTH = 384    # Llama分词器会将一个中文字切分为多个token，因此需要放开一些最大长度，保证数据的完整性\n",
    "    input_ids, attention_mask, labels = [], [], []\n",
    "    instruction = tokenizer(\"\\n\".join([\"<|im_start|>system\", \"现在你要扮演皇帝身边的女人--甄嬛.<|im_end|>\" + \"\\n<|im_start|>user\\n\" + example[\"instruction\"] + example[\"input\"] + \"<|im_end|>\\n\"]).strip(), add_special_tokens=False)  # add_special_tokens 不在开头加 special_tokens\n",
    "    response = tokenizer(\"<|im_start|>assistant\\n\" + example[\"output\"] + \"<|im_end|>\\n\", add_special_tokens=False)\n",
    "    input_ids = instruction[\"input_ids\"] + response[\"input_ids\"] + [tokenizer.pad_token_id]\n",
    "    attention_mask = instruction[\"attention_mask\"] + response[\"attention_mask\"] + [1]  # 因为eos token咱们也是要关注的所以 补充为1\n",
    "    labels = [-100] * len(instruction[\"input_ids\"]) + response[\"input_ids\"] + [tokenizer.pad_token_id]  # Qwen的特殊构造就是这样的\n",
    "    if len(input_ids) > MAX_LENGTH:  # 做一个截断\n",
    "        input_ids = input_ids[:MAX_LENGTH]\n",
    "        attention_mask = attention_mask[:MAX_LENGTH]\n",
    "        labels = labels[:MAX_LENGTH]\n",
    "    return {\n",
    "        \"input_ids\": input_ids,\n",
    "        \"attention_mask\": attention_mask,\n",
    "        \"labels\": labels\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "84f870d6-73a9-4b0f-8abf-687b32224ad8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                 \r"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['input_ids', 'attention_mask', 'labels'],\n",
       "    num_rows: 3729\n",
       "})"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenized_id = ds.map(process_func, remove_columns=ds.column_names)\n",
    "tokenized_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1f7e15a0-4d9a-4935-9861-00cc472654b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<|im_start|>system\\n现在你要扮演皇帝身边的女人--甄嬛.<|im_end|>\\n<|im_start|>user\\n小姐，别的秀女都在求中选，唯有咱们小姐想被撂牌子，菩萨一定记得真真儿的——<|im_end|><|im_start|>assistant\\n嘘——都说许愿说破是不灵的。<|im_end|>\\n<|endoftext|>'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.decode(tokenized_id[0]['input_ids'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "97f16f66-324a-454f-8cc3-ef23b100ecff",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<|im_start|>assistant\\n你们俩话太多了，我该和温太医要一剂药，好好治治你们。<|im_end|>\\n<|endoftext|>'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.decode(list(filter(lambda x: x != -100, tokenized_id[1][\"labels\"])))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "424823a8-ed0d-4309-83c8-3f6b1cdf274c",
   "metadata": {},
   "source": [
    "# 创建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "170764e5-d899-4ef4-8c53-36f6dec0d198",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The model is automatically converting to bf16 for faster inference. If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\".\n",
      "Try importing flash-attention for faster inference...\n",
      "Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary\n",
      "Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm\n",
      "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency https://github.com/Dao-AILab/flash-attention\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading checkpoint shards: 100%|██████████| 8/8 [00:05<00:00,  1.38it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "QWenLMHeadModel(\n",
       "  (transformer): QWenModel(\n",
       "    (wte): Embedding(151936, 4096)\n",
       "    (drop): Dropout(p=0.0, inplace=False)\n",
       "    (rotary_emb): RotaryEmbedding()\n",
       "    (h): ModuleList(\n",
       "      (0-31): 32 x QWenBlock(\n",
       "        (ln_1): RMSNorm()\n",
       "        (attn): QWenAttention(\n",
       "          (c_attn): Linear4bit(in_features=4096, out_features=12288, bias=True)\n",
       "          (c_proj): Linear4bit(in_features=4096, out_features=4096, bias=False)\n",
       "          (attn_dropout): Dropout(p=0.0, inplace=False)\n",
       "        )\n",
       "        (ln_2): RMSNorm()\n",
       "        (mlp): QWenMLP(\n",
       "          (w1): Linear4bit(in_features=4096, out_features=11008, bias=False)\n",
       "          (w2): Linear4bit(in_features=4096, out_features=11008, bias=False)\n",
       "          (c_proj): Linear4bit(in_features=11008, out_features=4096, bias=False)\n",
       "        )\n",
       "      )\n",
       "    )\n",
       "    (ln_f): RMSNorm()\n",
       "  )\n",
       "  (lm_head): Linear(in_features=4096, out_features=151936, bias=False)\n",
       ")"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained('./qwen/Qwen-7B-Chat/',\n",
    "                                                trust_remote_code=True, \n",
    "                                                torch_dtype=torch.half, \n",
    "                                                device_map=\"sequential\",\n",
    "                                                low_cpu_mem_usage=True,   # 是否使用cpu加速模型加载\n",
    "                                                load_in_4bit=True,  # 是否在4位精度下加载模型。如果设置为True，则在4位精度下加载模型。\n",
    "                                                bnb_4bit_compute_dtype=torch.half,  # 4位精度计算的数据类型。这里设置为torch.half，表示使用半精度浮点数。\n",
    "                                                bnb_4bit_quant_type=\"nf4\", # 4位精度量化的类型。这里设置为\"nf4\"，表示使用nf4量化类型。\n",
    "                                                bnb_4bit_use_double_quant=True  # 是否使用双精度量化。如果设置为True，则使用双精度量化。\n",
    "                                                )\n",
    "model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "285ec2ed",
   "metadata": {},
   "source": [
    "### 查看各个层的最终量化后的精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7a049113",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transformer.wte.weight torch.Size([151936, 4096]) torch.float16\n",
      "transformer.h.0.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.0.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.0.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.0.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.0.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.0.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.0.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.0.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.1.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.1.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.1.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.1.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.1.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.1.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.1.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.1.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.2.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.2.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.2.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.2.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.2.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.2.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.2.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.2.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.3.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.3.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.3.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.3.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.3.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.3.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.3.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.3.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.4.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.4.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.4.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.4.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.4.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.4.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.4.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.4.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.5.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.5.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.5.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.5.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.5.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.5.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.5.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.5.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.6.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.6.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.6.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.6.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.6.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.6.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.6.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.6.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.7.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.7.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.7.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.7.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.7.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.7.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.7.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.7.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.8.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.8.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.8.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.8.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.8.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.8.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.8.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.8.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.9.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.9.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.9.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.9.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.9.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.9.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.9.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.9.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.10.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.10.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.10.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.10.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.10.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.10.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.10.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.10.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.11.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.11.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.11.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.11.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.11.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.11.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.11.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.11.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.12.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.12.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.12.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.12.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.12.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.12.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.12.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.12.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.13.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.13.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.13.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.13.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.13.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.13.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.13.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.13.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.14.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.14.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.14.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.14.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.14.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.14.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.14.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.14.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.15.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.15.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.15.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.15.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.15.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.15.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.15.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.15.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.16.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.16.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.16.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.16.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.16.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.16.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.16.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.16.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.17.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.17.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.17.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.17.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.17.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.17.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.17.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.17.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.18.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.18.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.18.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.18.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.18.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.18.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.18.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.18.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.19.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.19.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.19.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.19.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.19.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.19.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.19.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.19.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.20.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.20.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.20.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.20.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.20.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.20.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.20.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.20.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.21.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.21.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.21.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.21.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.21.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.21.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.21.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.21.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.22.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.22.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.22.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.22.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.22.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.22.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.22.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.22.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.23.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.23.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.23.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.23.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.23.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.23.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.23.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.23.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.24.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.24.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.24.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.24.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.24.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.24.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.24.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.24.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.25.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.25.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.25.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.25.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.25.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.25.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.25.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.25.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.26.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.26.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.26.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.26.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.26.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.26.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.26.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.26.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.27.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.27.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.27.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.27.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.27.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.27.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.27.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.27.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.28.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.28.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.28.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.28.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.28.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.28.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.28.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.28.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.29.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.29.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.29.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.29.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.29.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.29.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.29.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.29.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.30.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.30.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.30.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.30.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.30.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.30.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.30.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.30.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.31.ln_1.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.31.attn.c_attn.weight torch.Size([25165824, 1]) torch.uint8\n",
      "transformer.h.31.attn.c_attn.bias torch.Size([12288]) torch.float16\n",
      "transformer.h.31.attn.c_proj.weight torch.Size([8388608, 1]) torch.uint8\n",
      "transformer.h.31.ln_2.weight torch.Size([4096]) torch.float16\n",
      "transformer.h.31.mlp.w1.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.31.mlp.w2.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.h.31.mlp.c_proj.weight torch.Size([22544384, 1]) torch.uint8\n",
      "transformer.ln_f.weight torch.Size([4096]) torch.float16\n",
      "lm_head.weight torch.Size([151936, 4096]) torch.float16\n"
     ]
    }
   ],
   "source": [
    "for name, param in model.named_parameters():\n",
    "    print(name, param.shape, param.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d28616e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.hf_device_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "796d3bea-0153-42f3-ad13-00b5393fc02d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "model.enable_input_require_grads() # 开启梯度检查点时，要执行该方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f808b05c-f2cb-48cf-a80d-0c42be6051c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float16"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b500984",
   "metadata": {},
   "source": [
    "### 配置LoRA时,打印model所有参数层名,方便观察target_modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "379a46a7-8042-4001-b4c3-af9c7f1b1b58",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "transformer.wte.weight\n",
      "transformer.h.0.ln_1.weight\n",
      "transformer.h.0.attn.c_attn.weight\n",
      "transformer.h.0.attn.c_attn.bias\n",
      "transformer.h.0.attn.c_proj.weight\n",
      "transformer.h.0.ln_2.weight\n",
      "transformer.h.0.mlp.w1.weight\n",
      "transformer.h.0.mlp.w2.weight\n",
      "transformer.h.0.mlp.c_proj.weight\n",
      "transformer.h.1.ln_1.weight\n",
      "transformer.h.1.attn.c_attn.weight\n",
      "transformer.h.1.attn.c_attn.bias\n",
      "transformer.h.1.attn.c_proj.weight\n",
      "transformer.h.1.ln_2.weight\n",
      "transformer.h.1.mlp.w1.weight\n",
      "transformer.h.1.mlp.w2.weight\n",
      "transformer.h.1.mlp.c_proj.weight\n",
      "transformer.h.2.ln_1.weight\n",
      "transformer.h.2.attn.c_attn.weight\n",
      "transformer.h.2.attn.c_attn.bias\n",
      "transformer.h.2.attn.c_proj.weight\n",
      "transformer.h.2.ln_2.weight\n",
      "transformer.h.2.mlp.w1.weight\n",
      "transformer.h.2.mlp.w2.weight\n",
      "transformer.h.2.mlp.c_proj.weight\n",
      "transformer.h.3.ln_1.weight\n",
      "transformer.h.3.attn.c_attn.weight\n",
      "transformer.h.3.attn.c_attn.bias\n",
      "transformer.h.3.attn.c_proj.weight\n",
      "transformer.h.3.ln_2.weight\n",
      "transformer.h.3.mlp.w1.weight\n",
      "transformer.h.3.mlp.w2.weight\n",
      "transformer.h.3.mlp.c_proj.weight\n",
      "transformer.h.4.ln_1.weight\n",
      "transformer.h.4.attn.c_attn.weight\n",
      "transformer.h.4.attn.c_attn.bias\n",
      "transformer.h.4.attn.c_proj.weight\n",
      "transformer.h.4.ln_2.weight\n",
      "transformer.h.4.mlp.w1.weight\n",
      "transformer.h.4.mlp.w2.weight\n",
      "transformer.h.4.mlp.c_proj.weight\n",
      "transformer.h.5.ln_1.weight\n",
      "transformer.h.5.attn.c_attn.weight\n",
      "transformer.h.5.attn.c_attn.bias\n",
      "transformer.h.5.attn.c_proj.weight\n",
      "transformer.h.5.ln_2.weight\n",
      "transformer.h.5.mlp.w1.weight\n",
      "transformer.h.5.mlp.w2.weight\n",
      "transformer.h.5.mlp.c_proj.weight\n",
      "transformer.h.6.ln_1.weight\n",
      "transformer.h.6.attn.c_attn.weight\n",
      "transformer.h.6.attn.c_attn.bias\n",
      "transformer.h.6.attn.c_proj.weight\n",
      "transformer.h.6.ln_2.weight\n",
      "transformer.h.6.mlp.w1.weight\n",
      "transformer.h.6.mlp.w2.weight\n",
      "transformer.h.6.mlp.c_proj.weight\n",
      "transformer.h.7.ln_1.weight\n",
      "transformer.h.7.attn.c_attn.weight\n",
      "transformer.h.7.attn.c_attn.bias\n",
      "transformer.h.7.attn.c_proj.weight\n",
      "transformer.h.7.ln_2.weight\n",
      "transformer.h.7.mlp.w1.weight\n",
      "transformer.h.7.mlp.w2.weight\n",
      "transformer.h.7.mlp.c_proj.weight\n",
      "transformer.h.8.ln_1.weight\n",
      "transformer.h.8.attn.c_attn.weight\n",
      "transformer.h.8.attn.c_attn.bias\n",
      "transformer.h.8.attn.c_proj.weight\n",
      "transformer.h.8.ln_2.weight\n",
      "transformer.h.8.mlp.w1.weight\n",
      "transformer.h.8.mlp.w2.weight\n",
      "transformer.h.8.mlp.c_proj.weight\n",
      "transformer.h.9.ln_1.weight\n",
      "transformer.h.9.attn.c_attn.weight\n",
      "transformer.h.9.attn.c_attn.bias\n",
      "transformer.h.9.attn.c_proj.weight\n",
      "transformer.h.9.ln_2.weight\n",
      "transformer.h.9.mlp.w1.weight\n",
      "transformer.h.9.mlp.w2.weight\n",
      "transformer.h.9.mlp.c_proj.weight\n",
      "transformer.h.10.ln_1.weight\n",
      "transformer.h.10.attn.c_attn.weight\n",
      "transformer.h.10.attn.c_attn.bias\n",
      "transformer.h.10.attn.c_proj.weight\n",
      "transformer.h.10.ln_2.weight\n",
      "transformer.h.10.mlp.w1.weight\n",
      "transformer.h.10.mlp.w2.weight\n",
      "transformer.h.10.mlp.c_proj.weight\n",
      "transformer.h.11.ln_1.weight\n",
      "transformer.h.11.attn.c_attn.weight\n",
      "transformer.h.11.attn.c_attn.bias\n",
      "transformer.h.11.attn.c_proj.weight\n",
      "transformer.h.11.ln_2.weight\n",
      "transformer.h.11.mlp.w1.weight\n",
      "transformer.h.11.mlp.w2.weight\n",
      "transformer.h.11.mlp.c_proj.weight\n",
      "transformer.h.12.ln_1.weight\n",
      "transformer.h.12.attn.c_attn.weight\n",
      "transformer.h.12.attn.c_attn.bias\n",
      "transformer.h.12.attn.c_proj.weight\n",
      "transformer.h.12.ln_2.weight\n",
      "transformer.h.12.mlp.w1.weight\n",
      "transformer.h.12.mlp.w2.weight\n",
      "transformer.h.12.mlp.c_proj.weight\n",
      "transformer.h.13.ln_1.weight\n",
      "transformer.h.13.attn.c_attn.weight\n",
      "transformer.h.13.attn.c_attn.bias\n",
      "transformer.h.13.attn.c_proj.weight\n",
      "transformer.h.13.ln_2.weight\n",
      "transformer.h.13.mlp.w1.weight\n",
      "transformer.h.13.mlp.w2.weight\n",
      "transformer.h.13.mlp.c_proj.weight\n",
      "transformer.h.14.ln_1.weight\n",
      "transformer.h.14.attn.c_attn.weight\n",
      "transformer.h.14.attn.c_attn.bias\n",
      "transformer.h.14.attn.c_proj.weight\n",
      "transformer.h.14.ln_2.weight\n",
      "transformer.h.14.mlp.w1.weight\n",
      "transformer.h.14.mlp.w2.weight\n",
      "transformer.h.14.mlp.c_proj.weight\n",
      "transformer.h.15.ln_1.weight\n",
      "transformer.h.15.attn.c_attn.weight\n",
      "transformer.h.15.attn.c_attn.bias\n",
      "transformer.h.15.attn.c_proj.weight\n",
      "transformer.h.15.ln_2.weight\n",
      "transformer.h.15.mlp.w1.weight\n",
      "transformer.h.15.mlp.w2.weight\n",
      "transformer.h.15.mlp.c_proj.weight\n",
      "transformer.h.16.ln_1.weight\n",
      "transformer.h.16.attn.c_attn.weight\n",
      "transformer.h.16.attn.c_attn.bias\n",
      "transformer.h.16.attn.c_proj.weight\n",
      "transformer.h.16.ln_2.weight\n",
      "transformer.h.16.mlp.w1.weight\n",
      "transformer.h.16.mlp.w2.weight\n",
      "transformer.h.16.mlp.c_proj.weight\n",
      "transformer.h.17.ln_1.weight\n",
      "transformer.h.17.attn.c_attn.weight\n",
      "transformer.h.17.attn.c_attn.bias\n",
      "transformer.h.17.attn.c_proj.weight\n",
      "transformer.h.17.ln_2.weight\n",
      "transformer.h.17.mlp.w1.weight\n",
      "transformer.h.17.mlp.w2.weight\n",
      "transformer.h.17.mlp.c_proj.weight\n",
      "transformer.h.18.ln_1.weight\n",
      "transformer.h.18.attn.c_attn.weight\n",
      "transformer.h.18.attn.c_attn.bias\n",
      "transformer.h.18.attn.c_proj.weight\n",
      "transformer.h.18.ln_2.weight\n",
      "transformer.h.18.mlp.w1.weight\n",
      "transformer.h.18.mlp.w2.weight\n",
      "transformer.h.18.mlp.c_proj.weight\n",
      "transformer.h.19.ln_1.weight\n",
      "transformer.h.19.attn.c_attn.weight\n",
      "transformer.h.19.attn.c_attn.bias\n",
      "transformer.h.19.attn.c_proj.weight\n",
      "transformer.h.19.ln_2.weight\n",
      "transformer.h.19.mlp.w1.weight\n",
      "transformer.h.19.mlp.w2.weight\n",
      "transformer.h.19.mlp.c_proj.weight\n",
      "transformer.h.20.ln_1.weight\n",
      "transformer.h.20.attn.c_attn.weight\n",
      "transformer.h.20.attn.c_attn.bias\n",
      "transformer.h.20.attn.c_proj.weight\n",
      "transformer.h.20.ln_2.weight\n",
      "transformer.h.20.mlp.w1.weight\n",
      "transformer.h.20.mlp.w2.weight\n",
      "transformer.h.20.mlp.c_proj.weight\n",
      "transformer.h.21.ln_1.weight\n",
      "transformer.h.21.attn.c_attn.weight\n",
      "transformer.h.21.attn.c_attn.bias\n",
      "transformer.h.21.attn.c_proj.weight\n",
      "transformer.h.21.ln_2.weight\n",
      "transformer.h.21.mlp.w1.weight\n",
      "transformer.h.21.mlp.w2.weight\n",
      "transformer.h.21.mlp.c_proj.weight\n",
      "transformer.h.22.ln_1.weight\n",
      "transformer.h.22.attn.c_attn.weight\n",
      "transformer.h.22.attn.c_attn.bias\n",
      "transformer.h.22.attn.c_proj.weight\n",
      "transformer.h.22.ln_2.weight\n",
      "transformer.h.22.mlp.w1.weight\n",
      "transformer.h.22.mlp.w2.weight\n",
      "transformer.h.22.mlp.c_proj.weight\n",
      "transformer.h.23.ln_1.weight\n",
      "transformer.h.23.attn.c_attn.weight\n",
      "transformer.h.23.attn.c_attn.bias\n",
      "transformer.h.23.attn.c_proj.weight\n",
      "transformer.h.23.ln_2.weight\n",
      "transformer.h.23.mlp.w1.weight\n",
      "transformer.h.23.mlp.w2.weight\n",
      "transformer.h.23.mlp.c_proj.weight\n",
      "transformer.h.24.ln_1.weight\n",
      "transformer.h.24.attn.c_attn.weight\n",
      "transformer.h.24.attn.c_attn.bias\n",
      "transformer.h.24.attn.c_proj.weight\n",
      "transformer.h.24.ln_2.weight\n",
      "transformer.h.24.mlp.w1.weight\n",
      "transformer.h.24.mlp.w2.weight\n",
      "transformer.h.24.mlp.c_proj.weight\n",
      "transformer.h.25.ln_1.weight\n",
      "transformer.h.25.attn.c_attn.weight\n",
      "transformer.h.25.attn.c_attn.bias\n",
      "transformer.h.25.attn.c_proj.weight\n",
      "transformer.h.25.ln_2.weight\n",
      "transformer.h.25.mlp.w1.weight\n",
      "transformer.h.25.mlp.w2.weight\n",
      "transformer.h.25.mlp.c_proj.weight\n",
      "transformer.h.26.ln_1.weight\n",
      "transformer.h.26.attn.c_attn.weight\n",
      "transformer.h.26.attn.c_attn.bias\n",
      "transformer.h.26.attn.c_proj.weight\n",
      "transformer.h.26.ln_2.weight\n",
      "transformer.h.26.mlp.w1.weight\n",
      "transformer.h.26.mlp.w2.weight\n",
      "transformer.h.26.mlp.c_proj.weight\n",
      "transformer.h.27.ln_1.weight\n",
      "transformer.h.27.attn.c_attn.weight\n",
      "transformer.h.27.attn.c_attn.bias\n",
      "transformer.h.27.attn.c_proj.weight\n",
      "transformer.h.27.ln_2.weight\n",
      "transformer.h.27.mlp.w1.weight\n",
      "transformer.h.27.mlp.w2.weight\n",
      "transformer.h.27.mlp.c_proj.weight\n",
      "transformer.h.28.ln_1.weight\n",
      "transformer.h.28.attn.c_attn.weight\n",
      "transformer.h.28.attn.c_attn.bias\n",
      "transformer.h.28.attn.c_proj.weight\n",
      "transformer.h.28.ln_2.weight\n",
      "transformer.h.28.mlp.w1.weight\n",
      "transformer.h.28.mlp.w2.weight\n",
      "transformer.h.28.mlp.c_proj.weight\n",
      "transformer.h.29.ln_1.weight\n",
      "transformer.h.29.attn.c_attn.weight\n",
      "transformer.h.29.attn.c_attn.bias\n",
      "transformer.h.29.attn.c_proj.weight\n",
      "transformer.h.29.ln_2.weight\n",
      "transformer.h.29.mlp.w1.weight\n",
      "transformer.h.29.mlp.w2.weight\n",
      "transformer.h.29.mlp.c_proj.weight\n",
      "transformer.h.30.ln_1.weight\n",
      "transformer.h.30.attn.c_attn.weight\n",
      "transformer.h.30.attn.c_attn.bias\n",
      "transformer.h.30.attn.c_proj.weight\n",
      "transformer.h.30.ln_2.weight\n",
      "transformer.h.30.mlp.w1.weight\n",
      "transformer.h.30.mlp.w2.weight\n",
      "transformer.h.30.mlp.c_proj.weight\n",
      "transformer.h.31.ln_1.weight\n",
      "transformer.h.31.attn.c_attn.weight\n",
      "transformer.h.31.attn.c_attn.bias\n",
      "transformer.h.31.attn.c_proj.weight\n",
      "transformer.h.31.ln_2.weight\n",
      "transformer.h.31.mlp.w1.weight\n",
      "transformer.h.31.mlp.w2.weight\n",
      "transformer.h.31.mlp.c_proj.weight\n",
      "transformer.ln_f.weight\n",
      "lm_head.weight\n"
     ]
    }
   ],
   "source": [
    "for name, param in model.named_parameters():\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13d71257-3c1c-4303-8ff8-af161ebc2cf1",
   "metadata": {},
   "source": [
    "# lora "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2d304ae2-ab60-4080-a80d-19cac2e3ade3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LoraConfig(peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=<TaskType.CAUSAL_LM: 'CAUSAL_LM'>, inference_mode=False, r=8, target_modules=['c_attn', 'c_proj', 'w1', 'w2'], lora_alpha=32, lora_dropout=0.1, fan_in_fan_out=False, bias='none', modules_to_save=None, init_lora_weights=True, layers_to_transform=None, layers_pattern=None)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from peft import LoraConfig, TaskType, get_peft_model\n",
    "\n",
    "config = LoraConfig(\n",
    "    task_type=TaskType.CAUSAL_LM, \n",
    "    target_modules=[\"c_attn\", \"c_proj\", \"w1\", \"w2\"],\n",
    "    inference_mode=False, # 训练模式\n",
    "    r=8, # Lora 秩\n",
    "    lora_alpha=32, # Lora alaph，具体作用参见 Lora 原理\n",
    "    lora_dropout=0.1# Dropout 比例\n",
    ")\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2c2489c5-eaab-4e1f-b06a-c3f914b4bf8e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LoraConfig(peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path='/CV/xhr_project/llm/model/Qwen-7B-Chat', revision=None, task_type=<TaskType.CAUSAL_LM: 'CAUSAL_LM'>, inference_mode=False, r=8, target_modules=['c_attn', 'c_proj', 'w1', 'w2'], lora_alpha=32, lora_dropout=0.1, fan_in_fan_out=False, bias='none', modules_to_save=None, init_lora_weights=True, layers_to_transform=None, layers_pattern=None)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = get_peft_model(model, config)\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ebf5482b-fab9-4eb3-ad88-c116def4be12",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "trainable params: 17,891,328 || all params: 4,501,213,184 || trainable%: 0.397477908035915\n"
     ]
    }
   ],
   "source": [
    "model.print_trainable_parameters()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca055683-837f-4865-9c57-9164ba60c00f",
   "metadata": {},
   "source": [
    "# 配置训练参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7e76bbff-15fd-4995-a61d-8364dc5e9ea0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "args = TrainingArguments(\n",
    "    output_dir=\"./output/Qwen\",\n",
    "    per_device_train_batch_size=2,\n",
    "    gradient_accumulation_steps=8,\n",
    "    logging_steps=10,\n",
    "    num_train_epochs=3,\n",
    "    gradient_checkpointing=True,\n",
    "    save_steps=100,\n",
    "    learning_rate=1e-4,\n",
    "    save_on_each_node=True,\n",
    "    optim=\"paged_adamw_32bit\"  # 优化器类型\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f142cb9c-ad99-48e6-ba86-6df198f9ed96",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "trainer = Trainer(\n",
    "    model=model,\n",
    "    args=args,\n",
    "    train_dataset=tokenized_id,\n",
    "    data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, padding=True),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "aec9bc36-b297-45af-99e1-d4c4d82be081",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...\n",
      "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...\n",
      "/CV/anaconda3/envs/llm23/lib/python3.9/site-packages/torch/nn/parallel/_functions.py:68: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector.\n",
      "  warnings.warn('Was asked to gather along dimension 0, but all '\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      \n",
       "      <progress value='33' max='348' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      [ 33/348 14:29 < 2:27:16, 0.04 it/s, Epoch 0.27/3]\n",
       "    </div>\n",
       "    <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       " <tr style=\"text-align: left;\">\n",
       "      <th>Step</th>\n",
       "      <th>Training Loss</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>3.329400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>2.433600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>2.343000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table><p>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trainer.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b2cee5d-3d58-4f82-8d26-0eb0158f61f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Both `max_new_tokens` (=512) and `max_length`(=512) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'system\\n现在你要扮演皇帝身边的女人--甄嬛.\\nuser\\n你是谁？\\nAssistant: 皇上好眼力，我是甄嬛，家父是大理寺少卿甄远道。\\n'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.eval()\n",
    "ipt = tokenizer(\"<|im_start|>system\\n现在你要扮演皇帝身边的女人--甄嬛.<|im_end|>\\n<|im_start|>user\\n{}<|im_end|>\\n\".format(\"你是谁？\", \"\").strip() + \"\\nAssistant: \", return_tensors=\"pt\").to(model.device)\n",
    "tokenizer.decode(model.generate(**ipt, max_length=512, do_sample=True, eos_token_id=tokenizer.eos_token_id, temperature=0.1)[0], skip_special_tokens=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fcc6977-efc2-4fab-8572-b6d3799d4448",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'吃饺子。臣妾亲自包的，味道顶好的。'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response, history = model.chat(tokenizer, \"今天晚上吃什么?\", history=[], system=\"现在你要扮演皇帝身边的女人--甄嬛.\")\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4df64a69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
